package com.maibow.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.HttpClient;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import com.maibow.util.Logger;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class HttpClientUtil {
    protected static final Logger logger = Logger.getLogger(HttpClientUtil.class);

    private static HttpClient client = new HttpClient();

    public static JSONObject httpClientPostAppVai(String valStr, Boolean isBase64, String url) throws IOException {
        if (isBase64.booleanValue())
            valStr = getBASE64(valStr);

        InputStream input = null;
        InputStreamReader isr = null;
        BufferedReader buffer = null;
        StringBuffer sb = null;
        String line = null;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("receipt-data", valStr);
        HttpPost httpPost = new HttpPost(url);
        StringEntity entity = new StringEntity(jsonObject.toString(), "UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        DefaultHttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(httpPost);
        int code = response.getStatusLine().getStatusCode();
        if (code == 200) {
            input = response.getEntity().getContent();
            isr = new InputStreamReader(input);
            buffer = new BufferedReader(isr, 10240);
            sb = new StringBuffer();
            while ((line = buffer.readLine()) != null) {
                sb.append(line);
            }
        } else {
            logger.warn("请救失败,重新POST...");
            // httpClientPostAppVai(valStr,
            // isBase64);
        }
        String result = getBASE64Decode(sb.toString());
        jsonObject = JSONObject.fromObject(result);
        return jsonObject;
    }

    /**
     * 
     * getBASE64(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param str
     * 标记：@return
     * 返回值：String
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public static String getBASE64(String str) {
        byte[] b = str.getBytes();
        String s = null;
        if (b != null)
            s = new BASE64Encoder().encode(b);

        return s;
    }

    /**
     * 
     * getBASE64Decode(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param str
     * 标记：@return
     * 返回值：String
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public static String getBASE64Decode(String str) {
        String ret = null;
        try {
            ret = new String(new BASE64Decoder().decodeBuffer(str));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }

    public static void main(String[] args) {
        String str = "ewoJInNpZ25hdHVyZSIgPSAiQW16Mlc3TTIrTExldS9OTU93ZmJROVlYY01oaHdaOWR1N3JnVGIvQzVtVHpXdEZhNmtPdlFzbnhGSXBqS1dqVCtnV1FWR3FmWllhVDFUYlkrcHNHL215NGxFenp0K3JJaUpUSXZ3MWwwb1FPMFNQcjh5Zk5mK2RtMGtMekN1OFpMUFBRVzBBNmxUQ0RJQ1RaSVVmNDFRUmsxRUNFbEVUMi9YN2ZqVUhuZDZhZ0FBQURWekNDQTFNd2dnSTdvQU1DQVFJQ0NCdXA0K1BBaG0vTE1BMEdDU3FHU0liM0RRRUJCUVVBTUg4eEN6QUpCZ05WQkFZVEFsVlRNUk13RVFZRFZRUUtEQXBCY0hCc1pTQkpibU11TVNZd0pBWURWUVFMREIxQmNIQnNaU0JEWlhKMGFXWnBZMkYwYVc5dUlFRjFkR2h2Y21sMGVURXpNREVHQTFVRUF3d3FRWEJ3YkdVZ2FWUjFibVZ6SUZOMGIzSmxJRU5sY25ScFptbGpZWFJwYjI0Z1FYVjBhRzl5YVhSNU1CNFhEVEUwTURZd056QXdNREl5TVZvWERURTJNRFV4T0RFNE16RXpNRm93WkRFak1DRUdBMVVFQXd3YVVIVnlZMmhoYzJWU1pXTmxhWEIwUTJWeWRHbG1hV05oZEdVeEd6QVpCZ05WQkFzTUVrRndjR3hsSUdsVWRXNWxjeUJUZEc5eVpURVRNQkVHQTFVRUNnd0tRWEJ3YkdVZ1NXNWpMakVMTUFrR0ExVUVCaE1DVlZNd2daOHdEUVlKS29aSWh2Y05BUUVCQ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0Nna2liM0pwWjJsdVlXd3RjSFZ5WTJoaGMyVXRaR0YwWlNJZ1BTQWlNakF4TkMwd09DMHdOeUF3TnpveU56bzFOU0JGZEdNdlIwMVVJanNLZlE9PSI7CgkiZW52aXJvbm1lbnQiID0gIlNhbmRib3giOwoJInBvZCIgPSAiMTAwIjsKCSJzaWduaW5nLXN0YXR1cyIgPSAiMCI7Cn0=";
        String url = "http://121.40.95.227:8081/meet/api/createtemporder";
        try {
            JSONObject js = httpClientPostAppVai(str, Boolean.valueOf(false), url);
            System.out.println(js);
            js = (JSONObject) js.get("receipt");
            System.out.println(js.get("product_id"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}